<?php

namespace App\Console\Commands;

use App\Models\Agent;
use App\Models\AgentRevenue;
use Illuminate\Database\Eloquent\Builder;

class RemoveTeamCommission extends ForEachAllAgents
{
    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'migrate:remove_team_commission';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = '剔除团队业绩里佣金';

    public function handle()
    {
        $this->foreachAllAgents(function (Agent $agent) {
            if ($agent->directInviter === null && $agent->indirectInviter === null) {
                return '';
            }

            // 用户的收入 (奖励 和 佣金), 扣除直接和间接邀请人的团队奖励和团队佣金, 先做加的, 再做减的, 为了防止出现负数
            // 原来减去的佣金 这里是 加, 原来加的 这里 减
            $agent
                ->revenue()
                ->whereIn('type', [
                    // 佣金
                    AgentRevenue::TYPE_REVOCATION_COMMISSION,
                    AgentRevenue::TYPE_REFUND_REVOCATION_COMMISSION,
                    // 奖励
                    AgentRevenue::TYPE_REVOCATION_DIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_REVOCATION_INDIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_REVOCATION_CULTIVATE_REWARDS,
                    AgentRevenue::TYPE_REFUND_REVOCATION_DIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_REFUND_REVOCATION_INDIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_REVOCATION_DAILY_ORDER_REWARD,
                    AgentRevenue::TYPE_REVOCATION_ORDER_REWARD_PER_MONTH,
                ])
                ->select(['id', 'agent_id', 'type', 'amount', 'created_at'])
                ->each(function (AgentRevenue $agentRevenue) use ($agent) {
                    $type = (int) $agentRevenue['type'];

                    if (in_array($type, [
                        AgentRevenue::TYPE_REVOCATION_COMMISSION,
                        AgentRevenue::TYPE_REFUND_REVOCATION_COMMISSION,
                    ], true)) {
                        $agent->directInviter && $agent->directInviter->incrementOrDecrementTeamCommission($agentRevenue['amount'], 'increment', $agentRevenue['created_at']);
                        $agent->indirectInviter && $agent->indirectInviter->incrementOrDecrementTeamCommission($agentRevenue['amount'], 'increment', $agentRevenue['created_at']);
                    } elseif (in_array($type, [
                        AgentRevenue::TYPE_REVOCATION_DIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_REVOCATION_INDIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_REVOCATION_CULTIVATE_REWARDS,
                        AgentRevenue::TYPE_REFUND_REVOCATION_DIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_REFUND_REVOCATION_INDIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_REVOCATION_DAILY_ORDER_REWARD,
                        AgentRevenue::TYPE_REVOCATION_ORDER_REWARD_PER_MONTH,
                    ], true)) {
                        $agent->directInviter && $agent->directInviter->incrementOrDecrementTeamReward($agentRevenue['amount'], 'increment', $agentRevenue['created_at']);
                        $agent->indirectInviter && $agent->indirectInviter->incrementOrDecrementTeamReward($agentRevenue['amount'], 'increment', $agentRevenue['created_at']);
                    }
                }, 5000);

            $agent
                ->revenue()
                ->whereIn('type', [
                    // 佣金
                    AgentRevenue::TYPE_COMMISSION,
                    // 奖励
                    AgentRevenue::TYPE_DIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_INDIRECT_INVITATION_REWARD,
                    AgentRevenue::TYPE_CULTIVATE_REWARDS,
                    AgentRevenue::TYPE_DAILY_ORDER_REWARD,
                    AgentRevenue::TYPE_ORDER_REWARD_PER_MONTH,
                ])
                ->select(['id', 'agent_id', 'type', 'amount', 'created_at'])
                ->each(function (AgentRevenue $agentRevenue) use ($agent) {
                    $type = (int) $agentRevenue['type'];

                    if ($type === AgentRevenue::TYPE_COMMISSION) {
                        $agent->directInviter && $agent->directInviter->incrementOrDecrementTeamCommission($agentRevenue['amount'], 'decrement', $agentRevenue['created_at']);
                        $agent->indirectInviter && $agent->indirectInviter->incrementOrDecrementTeamCommission($agentRevenue['amount'], 'decrement', $agentRevenue['created_at']);
                    } elseif (in_array($type, [
                        AgentRevenue::TYPE_DIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_INDIRECT_INVITATION_REWARD,
                        AgentRevenue::TYPE_CULTIVATE_REWARDS,
                        AgentRevenue::TYPE_DAILY_ORDER_REWARD,
                        AgentRevenue::TYPE_ORDER_REWARD_PER_MONTH,
                    ], true)) {
                        $agent->directInviter && $agent->directInviter->incrementOrDecrementTeamReward($agentRevenue['amount'], 'decrement', $agentRevenue['created_at']);
                        $agent->indirectInviter && $agent->indirectInviter->incrementOrDecrementTeamReward($agentRevenue['amount'], 'decrement', $agentRevenue['created_at']);
                    }
                }, 5000);

            return '';
        }, function (Builder $builder) {
            $builder
                ->select(['id', 'direct_inviter', 'indirect_inviter'])
                ->with(['directInviter:id', 'indirectInviter:id']);
        });
    }
}
